package com.hkts.naturalgas.base.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hkts.naturalgas.base.entity.RoastingDust;
import com.hkts.naturalgas.base.service.IRoastingDustService;
import com.hkts.naturalgas.utils.*;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.FileInputStream;
import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 焙烧除尘
 *
 * @author yck
 * @since 2021-05-31
 */
@RestController
@RequestMapping("/instrumentdata/roastingDust")
public class RoastingDustController {
    private final Logger logger = LoggerFactory.getLogger(RoastingDustController.class);

    @Autowired
    public IRoastingDustService iRoastingDustService;

    /**
     * 分页查询数据
     *
     * @param page         分页信息
     * @param roastingDust 查询条件
     * @return
     */
    @ResponseBody
    @GetMapping("/getRoastingDustPageList")
    public Result<Object> getHontUserList(Page<RoastingDust> page, RoastingDust roastingDust) {
        return ResultUtil.data(CustomPageUtils.getCustomPage(iRoastingDustService.selectPage(page, roastingDust)));
    }

    /**
     * 保存和修改公用的
     *
     * @param roastingDust 传递的实体
     * @return saveOrUpdate
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/roastingDustSave")
    public Result<Object> roastingDustSave(RoastingDust roastingDust) {
        iRoastingDustService.saveOrUpdate(roastingDust);
        return ResultUtil.success("保存成功");
    }

    /**
     * 根据id获取对象
     *
     * @param id 实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/roastingDustGetById")
    public Result<Object> roastingDustGetById(String id) {
        RoastingDust byId = iRoastingDustService.getById(id);
        return ResultUtil.data(byId);
    }

    /**
     * 根据id删除对象
     *
     * @param id 实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/roastingDustDelete")
    public Result<Object> hontUserDelete(String id) {
        iRoastingDustService.removeById(id);
        return ResultUtil.success("删除成功");
    }

    /**
     * 批量删除对象
     *
     * @param item 实体集合ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/roastingDustBatchDelete")
    public Result<Object> deleteBatchIds(String item) {
        List<String> ids = (List<String>) JSON.parse(item);
        iRoastingDustService.removeByIds(ids);
        return ResultUtil.success("删除成功");
    }

    @GetMapping("/getPageByTime")
    public Result<Object> getPageByTime(Page<RoastingDust> page, String startDate, String endDate) {
        return ResultUtil.data(CustomPageUtils.getCustomPage(iRoastingDustService.selectPageByTime(page, startDate, endDate)));
    }

    /**
     * 功能描述:
     * 数据录入
     *
     * @return:
     * @Author: 杨春科
     * @Date: 2021-06-08 9:41
     * @Description //TODO
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/importPaperData")
    @Transactional
    public Result<Object> importPaperData(String url) throws Exception {
        File excelFile = new File(url);
        HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(excelFile));
        HSSFSheet sheet = wb.getSheetAt(0);
        List<RoastingDust> list = new ArrayList<RoastingDust>(800);
        List<String> ids=new ArrayList<>();
        for (int i = 2; i < sheet.getLastRowNum(); i++) {

            HSSFRow row1 = sheet.getRow(i);
            if (row1.getCell(0) == null) {
                break;
            }
            Date dateCellValue = row1.getCell(0).getDateCellValue();
            System.out.println(dateCellValue);
            //本小时内时间列表
            List<LocalDateTime> allHourse = DataHandlerUtils.getAllHourse(dateCellValue);
            row1.getCell(0).setCellType(CellType.STRING);
            String time = row1.getCell(0).getStringCellValue();
            RoastingDust denitration = new RoastingDust();
            String id = String.valueOf(SnowFlakeUtil.getFlowIdInstance().nextId());
            ids.add(id);
            if(ids.contains(id)){
                System.out.println("id:"+id);
            }
            denitration.setId(id);
            denitration.setTime(allHourse.get(0));
            //1#电场一次电流
            row1.getCell(1).setCellType(CellType.STRING);
            denitration.setBsjhHvc140068(Float.parseFloat(row1.getCell(1).getStringCellValue()));
            //1#电场一次电压
            row1.getCell(2).setCellType(CellType.STRING);
            denitration.setBsjhHvc140067(Float.parseFloat(row1.getCell(2).getStringCellValue()));
            //2电场一次电压
            row1.getCell(3).setCellType(CellType.STRING);
            denitration.setBsjhHvc240067(Float.parseFloat(row1.getCell(3).getStringCellValue()));
            //2电场一次电流
            row1.getCell(4).setCellType(CellType.STRING);
            denitration.setBsjhHvc240068(Float.parseFloat(row1.getCell(4).getStringCellValue()));
            //3电场一次电压
            row1.getCell(5).setCellType(CellType.STRING);
            denitration.setBsjhHvc340067(Float.parseFloat(row1.getCell(5).getStringCellValue()));
            //3电场一次电流
            row1.getCell(6).setCellType(CellType.STRING);
            denitration.setBsjhHvc340068(Float.parseFloat(row1.getCell(6).getStringCellValue()));
            //4电场一次电压
            row1.getCell(7).setCellType(CellType.STRING);
            denitration.setBsjhHvc440067(Float.parseFloat(row1.getCell(7).getStringCellValue()));
            //4电场一次电流
            row1.getCell(8).setCellType(CellType.STRING);
            denitration.setBsjhHvc440068(Float.parseFloat(row1.getCell(8).getStringCellValue()));
            list.add(denitration);
            //5电场一次电压
            row1.getCell(9).setCellType(CellType.STRING);
            denitration.setBsjhHvc540067(Float.parseFloat(row1.getCell(9).getStringCellValue()));
            //5电场一次电流
            row1.getCell(10).setCellType(CellType.STRING);
            denitration.setBsjhHvc540068(Float.parseFloat(row1.getCell(10).getStringCellValue()));

            //6电场一次电压
            row1.getCell(11).setCellType(CellType.STRING);
            denitration.setBsjhHvc640067(Float.parseFloat(row1.getCell(11).getStringCellValue()));

            //6电场一次电流
            row1.getCell(12).setCellType(CellType.STRING);
            denitration.setBsjhHvc640068(Float.parseFloat(row1.getCell(12).getStringCellValue()));
            //喷淋塔进口温度
            row1.getCell(13).setCellType(CellType.STRING);
            denitration.setBsjhAi1(Float.parseFloat(row1.getCell(13).getStringCellValue()));
            //喷淋塔出口温度
            row1.getCell(14).setCellType(CellType.STRING);
            denitration.setBsjhAi2(Float.parseFloat(row1.getCell(14).getStringCellValue()));
            list.add(denitration);
            iRoastingDustService.save(denitration);
            //Thread.sleep(5);
        }
      // iRoastingDustService.saveBatch(list);
        return ResultUtil.success("处理成功！");
    }
   /** 功能描述:
   *  除尘数据处理 每小时内的数据转为每分钟
   * @return:
   * @Author: 杨春科
   * @Date: 2021-06-08 10:26
   * @Description //TODO
   */
   @ResponseBody
   @RequestMapping(method = RequestMethod.POST, value = "/dataHndler")
   @Transactional
   public Result dataHndler() throws ParseException {
       RoastingDust denitration = new RoastingDust();
       List<RoastingDust> roastingDusts = iRoastingDustService.selectList(denitration);
       List<String> collect = roastingDusts.stream().map(RoastingDust::getId).collect(Collectors.toList());
       System.out.println("删除数据");
       iRoastingDustService.deleteAll(collect);
     for(int i=0;i<roastingDusts.size();i++){
         if(i!=roastingDusts.size()-1){
             LocalDateTime time = roastingDusts.get(i).getTime();
             System.out.println(time);
             Date date = Date.from( time.atZone( ZoneId.systemDefault()).toInstant());
             List<LocalDateTime> allHourse = DataHandlerUtils.getAllHourse(date);
            //根据算法得到一个小时内的所有数据
             //冷却塔入口温度
             List<Float> dataBsjhAi1 = BezierPoint.getData(roastingDusts.get(i).getBsjhAi1(), roastingDusts.get(i + 1).getBsjhAi1());
             //冷却塔出口温度
             List<Float> dataBsjhAi2 = BezierPoint.getData(roastingDusts.get(i).getBsjhAi2(), roastingDusts.get(i + 1).getBsjhAi2());
             //1电场一次电压
             List<Float> dataBsjhHvc140067 = BezierPoint.getData(roastingDusts.get(i).getBsjhHvc140067(), roastingDusts.get(i + 1).getBsjhHvc140067());
             //1电场一次电流
             List<Float> dataBsjhHvc140068 = BezierPoint.getData(roastingDusts.get(i).getBsjhHvc140068(), roastingDusts.get(i + 1).getBsjhHvc140068());

             //2电场一次电压
             List<Float> dataBsjhHvc240067 = BezierPoint.getData(roastingDusts.get(i).getBsjhHvc240067(), roastingDusts.get(i + 1).getBsjhHvc240067());
             //2电场一次电流
             List<Float> dataBsjhHvc240068 = BezierPoint.getData(roastingDusts.get(i).getBsjhHvc240068(), roastingDusts.get(i + 1).getBsjhHvc240068());
             //3电场一次电压
             List<Float> dataBsjhHvc340067 = BezierPoint.getData(roastingDusts.get(i).getBsjhHvc340067(), roastingDusts.get(i + 1).getBsjhHvc340067());
             //3电场一次电流
             List<Float> dataBsjhHvc340068 = BezierPoint.getData(roastingDusts.get(i).getBsjhHvc340068(), roastingDusts.get(i + 1).getBsjhHvc340068());
             //4电场一次电压
             List<Float> dataBsjhHvc440067 = BezierPoint.getData(roastingDusts.get(i).getBsjhHvc440067(), roastingDusts.get(i + 1).getBsjhHvc440067());
             //4电场一次电流
             List<Float> dataBsjhHvc440068 = BezierPoint.getData(roastingDusts.get(i).getBsjhHvc440068(), roastingDusts.get(i + 1).getBsjhHvc440068());
             //5电场一次电压
             List<Float> dataBsjhHvc540067 = BezierPoint.getData(roastingDusts.get(i).getBsjhHvc540067(), roastingDusts.get(i + 1).getBsjhHvc540067());
             //5电场一次电流
             List<Float> dataBsjhHvc540068 = BezierPoint.getData(roastingDusts.get(i).getBsjhHvc540068(), roastingDusts.get(i + 1).getBsjhHvc540068());
           //5电场一次电压
             List<Float> dataBsjhHvc640067 = BezierPoint.getData(roastingDusts.get(i).getBsjhHvc640067(), roastingDusts.get(i + 1).getBsjhHvc640067());
             //5电场一次电流
             List<Float> dataBsjhHvc640068 = BezierPoint.getData(roastingDusts.get(i).getBsjhHvc640068(), roastingDusts.get(i + 1).getBsjhHvc640068());
             //进行循环处理
             List<RoastingDust> list=new ArrayList<RoastingDust>();
             for(int j=0;j<dataBsjhAi1.size();j++){
                 if(j==0){
                     roastingDusts.get(i).setBsjhAi1(DataHandlerUtils.getRundomFloat(roastingDusts.get(i).getBsjhAi1()));
                     roastingDusts.get(i).setBsjhAi2(DataHandlerUtils.getRundomFloat(roastingDusts.get(i).getBsjhAi2()));
                     roastingDusts.get(i).setId(String.valueOf(SnowFlakeUtil.getFlowIdInstance().nextId()));
                     list.add(roastingDusts.get(i));
                 }else{
                     RoastingDust dust=new RoastingDust();
                     dust.setTime(allHourse.get(j));
                     dust.setId(String.valueOf(SnowFlakeUtil.getFlowIdInstance().nextId()));
                     dust.setBsjhHvc240068(dataBsjhHvc240068.get(j));
                     dust.setBsjhHvc240067(dataBsjhHvc240067.get(j));
                     dust.setBsjhHvc140067(dataBsjhHvc140067.get(j));
                     dust.setBsjhHvc140068(dataBsjhHvc140068.get(j));
                     dust.setBsjhHvc340068(dataBsjhHvc340068.get(j));
                     dust.setBsjhHvc340067(dataBsjhHvc340067.get(j));
                     dust.setBsjhHvc440067(dataBsjhHvc440067.get(j));
                     dust.setBsjhHvc440068(dataBsjhHvc440068.get(j));
                     dust.setBsjhHvc540067(dataBsjhHvc540067.get(j));
                     dust.setBsjhHvc540068(dataBsjhHvc540068.get(j));
                     dust.setBsjhHvc640067(dataBsjhHvc640067.get(j));
                     dust.setBsjhHvc640068(dataBsjhHvc640068.get(j));
                     dust.setBsjhAi1(dataBsjhAi1.get(j));
                     dust.setBsjhAi2(dataBsjhAi2.get(j));
                     list.add(dust);
                 }
             }
             iRoastingDustService.saveBatch(list);
         }else{
             iRoastingDustService.save(roastingDusts.get(i));
         }
     }
       return ResultUtil.success("处理成功！");
   }
   /** 功能描述:
   *
   * @return:
   * @Author: 杨春科
   * @Date: 2021-06-20 17:50
   * @Description //TODO  处理整数情况
   */
   @ResponseBody
   @RequestMapping(method = RequestMethod.POST, value = "/dataNeatHndler")
   @Transactional
   public Result dataNeatHndler() throws ParseException {
      iRoastingDustService.dataNeatHndler();
       return ResultUtil.success("处理成功！");
   }

   /** 功能描述:
   *字段赋值buf
   * @return:
   * @Author: 杨春科
   * @Date: 2021-06-20 19:14
   * @Description //TODO 字段赋值bug处理
   */
   @ResponseBody
   @RequestMapping(method = RequestMethod.POST, value = "/dataBugHndler")
   @Transactional
   public Result dataBugHndler() throws ParseException {
       iRoastingDustService.dataBugHndler();
       return ResultUtil.success("处理成功！");
   }
}
